package com.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil{

    public static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");

    public static SimpleDateFormat sdfMonth = new SimpleDateFormat("yyyy-MM");

    public static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy-MM-dd");

    public static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static String FORMAT_YEAR = "year";

    public static String FORMAT_MONTH = "month";

    public static String FORMAT_DAY = "day";

    public static String FORMAT_HOUR = "hour";

    public static String FORMAT_MINUTE = "minute";

    public static String FORMAT_SECOND = "second";

    public static String getYear(Date date){
        return sdfYear.format(date);
    }

    public static Date getYear(String date){
        try {
            return sdfYear.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMonth(Date date){
        return sdfMonth.format(date);
    }

    public static Date getMonth(String date){
        try {
            return sdfMonth.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getDay(Date date){
        return sdfDay.format(date);
    }

    public static Date getDay(String date){
        try {
            return sdfDay.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getTime(Date date){
        return sdfTime.format(date);
    }

    public static Date getTime(String date){
        try {
            return sdfTime.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据日期格式进行日期操作
     */
    public static Date getTime(Date date, String format, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        if (FORMAT_YEAR.equals(format)){
            calendar.add(Calendar.YEAR, num);
        }
        if (FORMAT_MONTH.equals(format)){
            calendar.add(Calendar.MONTH, num);
        }
        if (FORMAT_DAY.equals(format)){
            calendar.add(Calendar.DATE, num);
        }
        if (FORMAT_HOUR.equals(format)){
            calendar.add(Calendar.HOUR_OF_DAY, num);
        }
        if (FORMAT_MINUTE.equals(format)){
            calendar.add(Calendar.MINUTE, num);
        }
        if (FORMAT_SECOND.equals(format)){
            calendar.add(Calendar.SECOND, num);
        }
        return calendar.getTime();
    }

    /**
     * 根据当前月的第N天
     */
    public static Date getTime(Date date, int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, num);
        return calendar.getTime();
    }

    /**
     * 格式化日期
     */
    public static Date getTime(Date date, String format) {
        try {
            if (FORMAT_YEAR.equals(format)){
                String year = getYear(date);
                String time = year + "-01-01 00:00:00";
                return sdfTime.parse(time);
            }
            if (FORMAT_MONTH.equals(format)){
                String month = getMonth(date);
                String time = month + "-01 00:00:00";
                return sdfTime.parse(time);
            }
            if (FORMAT_DAY.equals(format)){
                String day = getDay(date);
                String time = day + " 00:00:00";
                return sdfTime.parse(time);
            }
            return date;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 自定义日期
     */
    public static Date getTime(int year, int month, int day, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DATE, day);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, second);
        return calendar.getTime();
    }

}
